package other.xsy.passlys.receivable;

import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEvent;
import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEventRequest;
import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEventResponse;
import com.rkhd.platform.sdk.data.model.ClaimDetails__c;
import com.rkhd.platform.sdk.data.model.Claim__c;
import com.rkhd.platform.sdk.data.model.InvoiceDetail__c;
import com.rkhd.platform.sdk.exception.ApiEntityServiceException;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.service.XoqlService;
import other.xsy.passlys.util.HCXObjectService;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 认领明细新增、编辑、修改时，同步修改相关的认领金额字段
 * @Author 黎炎松
 * 2024-08-08 15:59
 */
public class ClaimDetailRuleevent implements RuleEvent {

    private static HCXObjectService hcxObjectService = new HCXObjectService();
    private static final Logger logger = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "认领明细进行变更时,修改相关的认领金额字段 ==> ";

    /**
     * 触发规则触发事件
     * @param request
     * @return
     * @throws ScriptBusinessException
     */
    @Override
    public RuleEventResponse execute(RuleEventRequest request) throws ScriptBusinessException {
        logger.info(LOGGER_MSG + "触发开始");
        Long dataId = request.getDataId();
        try {
            ClaimDetails__c claimDetails = new ClaimDetails__c();
            claimDetails.setId(dataId);
            claimDetails = hcxObjectService.get(claimDetails);
            logger.info("claimDetails " + claimDetails);

            // 获取booking 计算相关的认领金额字段
            ReceivableService receivableService = new ReceivableService();
            receivableService.calcClaimAmounByBooking(Arrays.asList(claimDetails.getBooking__c()));

            // 回写主表booking,记录子表的bookingIds已经银行流水的ids (因为删除认领后无法关联上银行流水)
            Long claimId = claimDetails.getClaim__c();
            String sql = "select id,booking__c,claim__c.bankStatement__c bankStatement from claimDetails__c where claim__c = " + claimId;
            List<JSONObject> claimDetailList = hcxObjectService.batchXoqlQuery(sql);
            String bookingIds = claimDetailList.stream().map(v -> v.getString("booking__c")).distinct().collect(Collectors.joining(","));
            String bankStatementIds = claimDetailList.stream().map(v -> v.getString("bankStatement")).distinct().collect(Collectors.joining(","));

            Claim__c claim = new Claim__c();
            claim.setId(claimId);
            claim.setBookingStr__c(bookingIds + "|" + bankStatementIds);
            logger.info(LOGGER_MSG + "claim: " + claim);
            Boolean update = hcxObjectService.update(claim);
            logger.info(LOGGER_MSG + "update: " + update);

            logger.info(LOGGER_MSG + "执行结束");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        RuleEventResponse response = new RuleEventResponse();
        response.setSuccess(true);
        response.setMsg("success");
        return response;
    }
}
